package com.ruoyi.project.wms.controller;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.wms.mapper.WmsOrderMapper;
import com.ruoyi.project.wms.mapper.WmsOrderTypeMapper;
import com.ruoyi.project.wms.service.impl.WmsOrderTypeServiceImpl;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.common.zx.PageData;
import com.ruoyi.project.wms.service.impl.WmsOrderServiceImpl;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 物品订单Controller
 *
 * @author zx
 * @date 2023-05-29
 */
@RestController
@RequestMapping("/wms/order")
public class WmsOrderController extends BaseController {
    @Autowired
    private WmsOrderServiceImpl wmsOrderService;

    @Autowired
    private WmsOrderTypeMapper wmsOrderTypeMapper;

    @Autowired
    private WmsOrderMapper wmsOrderMapper;

    /**
     * 查询物品订单列表
     */
    @PreAuthorize("@ss.hasPermi('wms:order:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = wmsOrderService.selectWmsOrderList(pd);
        return getDataTable(list);
    }

    /**
     * 导出物品订单列表
     */
    @PreAuthorize("@ss.hasPermi('wms:order:export')")
    @Log(title = "物品订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageData pd) {
        List<PageData> list = wmsOrderService.selectWmsOrderList(pd);
        ExcelUtil<PageData> util = new ExcelUtil<PageData>(PageData.class);
        util.exportExcel(response, list, "物品订单数据");
    }

    /**
     * 获取物品订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wmsOrderService.selectWmsOrderById(id));
    }

    /**
     * 新增物品订单
     */
    @PreAuthorize("@ss.hasPermi('wms:order:add')")
    @Log(title = "物品订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PageData pd) {
        return toAjax(wmsOrderService.insertWmsOrder(pd));
    }

    /**
     * 修改物品订单
     */
    @PreAuthorize("@ss.hasPermi('wms:order:edit')")
    @Log(title = "物品订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PageData pd) {
        return toAjax(wmsOrderService.updateWmsOrder(pd));
    }

    /**
     * 删除物品订单
     */
    @PreAuthorize("@ss.hasPermi('wms:order:remove')")
    @Log(title = "物品订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wmsOrderService.deleteWmsOrderByIds(ids));
    }

    /**
     * 执行入库|出库
     */
    @PutMapping("/{id}")
    public AjaxResult updateOrderStatus(@PathVariable Long id) {
        PageData pageData = wmsOrderService.selectWmsOrderById(id);
        String state = pageData.getString("state");
        if (state.equals("END")) {
            return error("单据已执行,无需重复执行");
        }
        return toAjax(wmsOrderService.updateOrderStatus(id));
    }

    @GetMapping("/getOrderType")
    public AjaxResult getOrderType() {
        return AjaxResult.success(wmsOrderTypeMapper.selectOrderTypeList());
    }

    @GetMapping("/getOrderLine/{id}")
    public AjaxResult getOrderLine(@PathVariable Long id) {
        return AjaxResult.success(wmsOrderMapper.getOderLine(id));
    }


    @PostMapping("/PostOrderLine")
    public AjaxResult AddOrderLine(@RequestBody PageData pd) {
        boolean flag = false;
        try {
            flag = wmsOrderMapper.insertOrderLine(pd) > 0;
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return error("当前产品物料和批次号已存在");
            }
        }
        return toAjax(flag);
    }

    @PutMapping("/PutOrderLine")
    public AjaxResult updateOrderLine(@RequestBody PageData pd) {
        return toAjax(wmsOrderMapper.updateOrderLine(pd));
    }

    @DeleteMapping("/deleteLine")
    public AjaxResult deleteLine(@RequestBody List<PageData> pd) {
        return toAjax(wmsOrderMapper.deleteWmsOrderLineByIds(pd));
    }

    @DeleteMapping("/deleteLineOne")
    public AjaxResult deleteLineOne(@RequestBody PageData pd) {
        ArrayList<PageData> arrayList = new ArrayList<>();
        arrayList.add(pd);
        return toAjax(wmsOrderMapper.deleteWmsOrderLineByIds(arrayList));
    }

    @PostMapping("/getLineInfo")
    public AjaxResult getLineInfo(@RequestBody PageData pd) {
        return success(wmsOrderMapper.getLineInfo(pd));
    }

}
